package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.FloorDto;
import com.zzyl.entity.Floor;
import com.zzyl.mapper.FloorMapper;
import com.zzyl.service.BedService;
import com.zzyl.service.FloorService;
import com.zzyl.service.RoomService;
import com.zzyl.vo.FloorVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;


@Service

public class FloorServiceImpl implements FloorService {


    @Autowired
    RoomService roomService;

    @Autowired
    BedService bedService;

    @Autowired
    private FloorMapper floorMapper;


    @Override
    public List<FloorVo> getAllWithRoomAndBed() {
        return floorMapper.selectAllRoomAndBed();
    }

    @Override
    public List<FloorVo> selectAllByNur() {
        return floorMapper.selectAllByNur();
    }

    /**
     * 添加楼层
     *
     * @param floorDto
     */
    @Override
    public void addFloor(FloorDto floorDto) {
        Floor floor = new Floor();
        BeanUtils.copyProperties(floorDto, floor);
        floor.setCreateBy(1L);
        floor.setCreateTime(LocalDateTime.now());
        floorMapper.insertFloor(floor);
    }

    /**
     * 获取所有楼层
     *
     * @return
     */
    @Override
    public List<FloorVo> getAllFloor() {
        List<Floor> floors = floorMapper.selectAllFloor();
        List<FloorVo> floorVos = new ArrayList<>();
        floors.forEach(floor -> {
            FloorVo floorVo = BeanUtil.toBean(floor, FloorVo.class);
            floorVos.add(floorVo);
        });
        floorVos.sort(Comparator.comparingInt(FloorVo::getCode));
        return floorVos;
    }

    /**
     * 根据id删除楼层
     *
     * @param id
     */
    @Override
    public void deleteFloorById(Integer id) {
        floorMapper.deleteFloorById(id);
    }

    /**
     * 根据id查询楼层
     *
     * @param id
     * @return
     */
    @Override
    public FloorVo getFloorById(Integer id) {
        Floor floor = floorMapper.selectFloorById(id);
        return BeanUtil.toBean(floor, FloorVo.class);
    }

    /**
     * 修改楼层信息
     *
     * @param floorDto
     */
    @Override
    public void updateFloor(FloorDto floorDto) {
        Floor floor = BeanUtil.toBean(floorDto, Floor.class);
        floor.setCreateBy(2L);
        floor.setUpdateTime(LocalDateTime.now());
        floorMapper.updaetFloor(floor);
    }

}

